#ifndef _RENDERER_H_
#define _RENDERER_H_

#include <windows.h>
#include "../shapes.h"
#include "context.h"
#include "viewport.h"
#include "ForwardDeclarations.h"
//#include <boost/smart_ptr/shared_ptr.hpp>

class CRenderer
{
private:
	static CRenderer * _inst;
	static unsigned int __stdcall _thread_entrypoint(void * args);

	volatile unsigned int _is_running;

	static HWND _hwnd;
	CContext _ctx;
	HANDLE _render_thread_handle;
	CViewportPtr _vp;
//	boost::shared_ptr<CViewport> _vp;

	void _init();
	void _enterLoop();
	inline void _leaveLoop() {_ctx.swapBuffers();}

protected:
	CRenderer(HINSTANCE instance, const SRect & screen);
	CRenderer(const CRenderer & r);
	CRenderer & operator = (const CRenderer & r);
public:
//	inline static CRenderer * create(HINSTANCE instance) {return (_inst = new CRenderer(instance, SRect(0, 0, 1920, 1080)));}
	inline static CRenderer * create(HINSTANCE instance) {return (_inst = new CRenderer(instance, SRect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN))));}
	inline static CRenderer * getInstance() {return _inst;}
	inline const CViewportCPtr getViewport() const {return _vp;}
	inline const CContext & getContext() const {return _ctx;}
	inline static HWND & getHWND() {return _hwnd;}
	inline void run() const {ResumeThread(_render_thread_handle);}
};

#endif //_RENDERER_H_